package com.codingclub.oss.adapter;

import com.codingclub.oss.entity.FileInfo;
import com.codingclub.oss.enums.ErrCode;
import com.codingclub.oss.exception.BusinessException;
import com.codingclub.oss.util.MinioUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

public class MinioServerAdapter implements OssServerAdapter {
    @Resource
    private  MinioUtil minioUtil;
    @Override
    public void createBucket(String bucket) {
        try {
            minioUtil.createBucket(bucket);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        try {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }

    }

    @Override
    public List<String> getAllBucket() {
        try {
            return minioUtil.getAllBucket();
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }
    }

    @Override
    public List<FileInfo> getAllFile(String bucket) {
        try {
            return minioUtil.getAllFile(bucket);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }
    }

    @Override
    public InputStream downLoad(String bucket, String objectName) {
        try {
            return minioUtil.downLoad(bucket, objectName);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void deleteBucket(String bucket) {
        try {
            minioUtil.deleteBucket(bucket);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void deleteObject(String bucket, String objectName) {
        try {
            minioUtil.deleteObject(bucket, objectName);
        } catch (Exception e) {
            throw new BusinessException(ErrCode.SYSTEM_ERROR);
        }

    }
}
